angular.module('goods-add', ['img-picker','div-slide-in','head-toast'])

.controller 'GoodsAddCtrl', ($ionicLoading,$scope, $state, $http, $ionicHistory, $stateParams, newAddress, wfCache,$ionicViewSwitcher,$ionicPopup,$log,userData,showToast) ->
    getareaData = ->
        $http.get('/seller/',params:{action:'areadata'})
        .success (resp) ->
            console.log(resp)
            if resp.status == '0'
                $scope.areaData = resp.body
                console.log $scope.areaData
                $scope.provinceData = $scope.areaData[0].province
                $scope.cityData = $scope.areaData[1].city
                $scope.currentCity = []
                _.each $scope.cityData,(city) ->
                    if $scope.goods.province == city.provinceid
                        $scope.currentCity.push city
                console.log $scope.currentCity
        .error ->
            showToast '服务器处理失败'

    getSort = ->
        $http.get '/updshop/',params:{action:'productsort',userid:userData.getToken()}
        .success (resp) ->
            if resp.status == '0'
                $scope.Allsort = resp.body
                console.log $scope.Allsort
                _.each $scope.Allsort,(first)->
                    if first.firstsortid == $scope.goods.firstsort
                        $scope.secondSort = first.childsort
                console.log $scope.secondSort
                _.each $scope.secnondSort,(second)->
                    if second.secondsortid == $scope.goods.secondsort
                        $scope.thirdSort = second.childsort
                console.log $scope.thirdSort
        .error (e)->
            showToast '服务器处理失败'

    _.extend $scope,
        goods:
            action:'saveproduct'
            userid:userData.getToken()
            id:0
            title:undefined
            productno:undefined
            saleprice:undefined
            productsku:undefined
            mainimg:[]
            txtimg:[]
            unitage:undefined #计量
            brand:undefined #品牌
            countries:undefined  #原产国
            model:undefined  #规格型号
            conditions:undefined  #规格参数
            service:undefined  #售后服务
            province:undefined  #发货省(ID)
            city:undefined  #发货地区(市ID)
            sendmoney:undefined #运费
            firstsort:undefined #一级分类ID
            secondsort:undefined #二级分类ID
            thirdsort:undefined #三级分类ID
            isnew:1 #最新上架(0否1是)
            ishot:1 #热门促销(0否1是)
            istop:1 #店长推荐(0否1是)

        formValidation: ->
            if $scope.goods.productno.match(/(^[A-Za-z0-9]+$)/) == null
                showToast '请输入正确的商品编号,由数字或字母组成.'
                return false
            else if $scope.goods.productsku.match(/^[0-9]+([.][0-9]+){0,1}$/) == null
                showToast '请输入正确的商品库存,由数字组成.'
                return false
            else if $scope.goods.saleprice.match(/^[0-9]+([.][0-9]+){0,1}$/) == null
                showToast '请输入正确的销售价,由数字组成.'
                return false
            else if $scope.goods.province == undefined
                showToast '请选择省份'
                return false
            else if $scope.goods.city == undefined
                showToast '请选择城市'
                return false
#            else if $scope.goods.firstsort == undefined
#                showToast '请选择分类'  #非必选项
#                return false
            else
                return true
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()

        getProvince:() ->
            console.log $scope.goods.province
            $scope.currentCity = []
            _.each $scope.cityData,(city) ->
                if $scope.goods.province == city.provinceid
                    $scope.currentCity.push city
            console.log $scope.currentCity
        getCity:() ->
            console.log $scope.goods.city
        getFirst: ->
            console.log $scope.goods.firstsort
            _.each $scope.Allsort,(first)->
                if first.firstsortid == $scope.goods.firstsort
                    $scope.secondSort = first.childsort
            console.log $scope.secondSort
        getSecond: ->
            console.log $scope.goods.secondsort
            _.each $scope.secnondSort,(second)->
                if second.secondsortid == $scope.goods.secondsort
                    $scope.thirdSort = second.childsort
            console.log $scope.thirdSort
        getThird: ->
            console.log $scope.goods.thirdsort
        changeNewOn: ->
            $scope.goods.isnew = 1
        changeNewOff: ->
            $scope.goods.isnew = 0
        changeHotOn: ->
            $scope.goods.ishot = 1
        changeHotOff: ->
            $scope.goods.ishot = 0
        changeTopOn: ->
            $scope.goods.istop = 1
        changeTopOff: ->
            $scope.goods.istop = 0
        goSave: ->
            if  $scope.formValidation()
                $ionicLoading.show()
                $http.post('/utf-8data/',$scope.goods)
                .success (resp) ->
                    if resp?.status == '0'
                        showToast '添加成功'
                        $scope.goods = {
                            action:'saveproduct'
                            userid:userData.getToken()
                            id:0
                            title:undefined
                            productno:undefined
                            saleprice:undefined
                            productsku:undefined
                            mainimg:[]
                            txtimg:[]
                            unitage:undefined #计量
                            brand:undefined #品牌
                            countries:undefined  #原产国
                            model:undefined  #规格型号
                            conditions:undefined  #规格参数
                            service:undefined  #售后服务
                            province:undefined  #发货省(ID)
                            city:undefined  #发货地区(市ID)
                            sendmoney:undefined #运费
                            firstsort:undefined #一级分类ID
                            secondsort:undefined #二级分类ID
                            thirdsort:undefined #三级分类ID
                            isnew:undefined #最新上架(0否1是)
                            ishot:undefined #热门促销(0否1是)
                            istop:undefined #店长推荐(0否1是)
                        }
                        $ionicViewSwitcher.nextDirection('back')
                        $ionicHistory.goBack()
                    else
                        showToast resp.msg
                .error ->
                    showToast '服务器处理失败'
                .finally ->
                    $ionicLoading.hide()

    $scope.$on '$ionicView.enter', ->
        getareaData()
        getSort()




